export const formatTime = (date: Date) => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return (
    [year, month, day].map(formatNumber).join('/') +
    ' ' +
    [hour, minute, second].map(formatNumber).join(':')
  )
}

const formatNumber = (n: number) => {
  const s = n.toString()
  return s[1] ? s : '0' + s
}


/**
 * 随机算法
 * n里抽取min - max个数字
 * 
 *  */ 

export const randomNum = (min: number, max: number, n: number, list?: never[]) => {
  let arr = [], result: any[] = [], indexResult = [];
  for (let i = min - 1; i < max; i++) {
    arr.push(i);
  }
  if (n > arr.length) {
    console.warn('随机数n不可大于数组arr的长度');
    return;
  }

  for (let i = 0; i < n; i++) {
    let id = Math.ceil(Math.random() * (arr.length - 1));
    if (indexResult.indexOf(arr[id]) === -1) {
      indexResult.push(arr[id]);
      list && result.push(list[arr[id]])
    } else {
      i = i - 1;
      continue;
    }
  }
  // console.log(indexResult)
  // 返回完整数组 or 索引
  if (list && list.length) {
    return result;
  }
  return []
}